package mymyoux.io.sockets.clients.clientsClasses
{
	import mymyoux.debug.log;
	import mymyoux.io.sockets.mapping.Telegramme;
	import mymyoux.signals.MetaSignal;
	import mymyoux.signals.Signal;
	import mymyoux.utils.Tampon;


	public class MRoom
	{
		protected var _connected:Boolean;
		protected var _name:String;
		protected var _socket:MBinarySocket;
		protected var _telegrammeConnexion:Telegramme;
		protected var _tampon:Tampon;
		protected var _signalData:MetaSignal;
		public function MRoom(name:String)
		{
			_name = name;
			_connected = false;
			_telegrammeConnexion = new Telegramme();
			_telegrammeConnexion.command = Telegramme.GET_ROOM;
			_telegrammeConnexion.room = name;
			_tampon = new Tampon();
			_signalData = new MetaSignal(Object);
		}
		public function get connected():Boolean
		{
			return _connected;
		}
		public function get socket():MBinarySocket
		{
			return _socket;
		}
		public function set socket(value:MBinarySocket):void
		{
			trace("CREATION SOCKET["+_name+"]");
			if(!value.equals(socket))
			{
				removeSignals();
				_socket = value;
				addSignals();
				if(_socket.connected)
				{
					trace("DEJA CONNECTEE");
					onConnexion();
				}else
				{
					_socket.connect();
				}
			}
		}
		public function write(data:Object, action:String = "default"):void
		{
			if(!_connected)
			{
				_tampon.add(data, action);
			}else
			{
				var t:Telegramme = new Telegramme();
				t.command = Telegramme.SEND_ROOM;
				t.room = _name;
				t.action = action;
				t.object = data;//telegramme;
				socket.write(t);
			}
		}
		public function get signalData():MetaSignal
		{
			return _signalData;
		}
		protected function onConnexion():void
		{
			socket.write(_telegrammeConnexion);
		}
		protected function onStatus(connected:Boolean):void
		{
			trace("STATUS CONNEXION["+connected+"] for "+_name);
			if(connected)
			{
				onConnexion();
			}
		}
		protected function onData(telegramme:Telegramme):void
		{
			switch(telegramme.command)
			{
				case Telegramme.GET_ROOM:
					trace("get_room:"+telegramme);
					//telegramme = telegramme.read() as Telegramme;
					if(telegramme.action == Telegramme.ROOM_OK)
					{
						_connected = true;
						while(_tampon.hasNext())
						{
							write.apply(this, _tampon.next());
						}
						trace("RoomConnected");
					}else
					{
						trace("RoomDisConnected");
						//TODO:Bad ROOM
					}
				break;
				case Telegramme.SEND_ROOM:
					trace("rec:SendData["+_name+"]:"+telegramme);
					_signalData.dispatch(telegramme.action, telegramme.value);
				break;
				default:
					trace("RoomData:Unknown_Command["+_name+"]:"+telegramme.read());
			}
		}
		protected function onError(error:Error):void
		{
			trace("Error[MRoom]:"+error);
		}
		protected function removeSignals():void
		{
			if(socket != null)
			{
				socket.signalStatus.remove(onStatus);
				socket.signalDataRoom.remove(_name, onData);
				socket.signalError.remove(onError);
			}
		}
		protected function addSignals():void
		{
			if(socket != null)
			{
				socket.signalStatus.add(onStatus);
				socket.signalDataRoom.add(_name, onData);
				socket.signalError.add(onError);
			}
		}
		
	}
}
import mymyoux.io.sockets.mapping.Telegramme;

class Tampon
{
	protected var _objects:Vector.<Object>;
	protected var _actions:Vector.<String>;
	public function Tampon()
	{
		_objects = new Vector.<Object>();
		_actions = new Vector.<String>(); 
	}
	public function add(t:Object, action:String):void
	{
		_objects.push(t);
		_actions.push(action);
	}
	public function hasNext():Boolean
	{
		return _objects.length > 0;
	}
	public function next():Array
	{
		return [_objects.shift(), _actions.shift()];
	}
}